package com.bosto.jiramanager;

import java.util.ArrayList;
import java.util.List;

import com.bosto.jiramanager.entity.Jira;
import com.bosto.jiramanager.entity.Contants;
import com.bosto.jiramanager.helper.SharedPreferencesHelper;
import com.bosto.jiramanager.helper.SqlService;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class JiraListActivity extends Activity {
	private RadioGroup rg;
	private RadioButton rb1,rb2,rb3,rb4,rb5;
	private SharedPreferencesHelper sph;
	private List<Jira> jiraUnS, jiraImp, jiraRew, jiraMer, jiraVer,jiraAll;

	private SqlService sqlHelper;
	private ListView listview;
	private Context context;
	@SuppressWarnings("rawtypes")
	private ArrayAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.showlistview);
		context = ((ApplicationClass) getApplication()).getContext();
		InitView();
		sph = new SharedPreferencesHelper(context, Contants.DBNAMESET);
		String dbName = sph.GetValue(Contants.DBCHOSEN);
		sqlHelper = new SqlService(context, dbName + Contants.DBNAMECOMMENT, 1);
		initTheJira();
		initAdapter();
		rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				case R.id.unsolve:
					adapter = new ArrayAdapter(JiraListActivity.this,
							R.layout.listviewtext, Transformation(jiraUnS));
					listview.setAdapter(adapter);
					break;
				case R.id.implement:
					adapter = new ArrayAdapter(JiraListActivity.this,
							R.layout.listviewtext, Transformation(jiraImp));
					listview.setAdapter(adapter);
					break;
				case R.id.reviewed:
					adapter = new ArrayAdapter(JiraListActivity.this,
							R.layout.listviewtext, Transformation(jiraRew));
					listview.setAdapter(adapter);
					break;
				case R.id.merged:
					adapter = new ArrayAdapter(JiraListActivity.this,
							R.layout.listviewtext, Transformation(jiraMer));
					listview.setAdapter(adapter);
					break;
				case R.id.verified:
					adapter = new ArrayAdapter(JiraListActivity.this,
							R.layout.listviewtext, Transformation(jiraVer));
					listview.setAdapter(adapter);
					break;
				default:
					break;
				}
			}
		});
listview.setOnItemClickListener(new OnItemClickListener() {

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		SelectDialog selectDialog = new SelectDialog(
				JiraListActivity.this, R.style.dialog);
		selectDialog.Id = position;

		selectDialog.setCanceledOnTouchOutside(true);
		selectDialog.show();
		
	}
});

	}

	void InitView() {
		rg = (RadioGroup) findViewById(R.id.stategroup);
		rb1 = (RadioButton) findViewById(R.id.unsolve);
		rb2 = (RadioButton) findViewById(R.id.implement);
		rb3 = (RadioButton) findViewById(R.id.reviewed);
		rb4 = (RadioButton) findViewById(R.id.merged);
		rb5 = (RadioButton) findViewById(R.id.verified);
		listview = (ListView) findViewById(R.id.jira_listview);
		rg.check(rb1.getId());
	}

	private void initTheJira() {
		jiraUnS = new ArrayList<Jira>();
		jiraImp = new ArrayList<Jira>();
		jiraRew = new ArrayList<Jira>();
		jiraMer = new ArrayList<Jira>();
		jiraVer = new ArrayList<Jira>();
		jiraAll = new ArrayList<Jira>();
		jiraAll = sqlHelper.getAllJira();
		jiraUnS = GetJiraByStatus(Contants.STATUS_OPEN);
		jiraImp = GetJiraByStatus(Contants.STATUS_IMPLEMENTED);
		jiraRew = GetJiraByStatus(Contants.STATUS_REVIEWED);
		jiraMer = GetJiraByStatus(Contants.STATUS_MERGERD);
		jiraVer = GetJiraByStatus(Contants.STATUS_VERIFIED);

	}

	private List<Jira> GetJiraByStatus(String status) {
		List<Jira> result = new ArrayList<Jira>();
		for (Jira p : jiraAll) {
			if (p.getStatus().equals(status)) {
				result.add(p);
			}
		}
		return result;
	}

	private List<String> Transformation(List<Jira> jiras) {
		List<String> result = new ArrayList<String>();
		for (Jira p : jiras) {
			String temp = p.getJiraNo() + Contants.COMMENT + p.getType();
			result.add(temp);
		}
		return result;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void initAdapter() {
		adapter = new ArrayAdapter(JiraListActivity.this,
				R.layout.listviewtext, Transformation(jiraUnS));
		listview.setAdapter(adapter);

	}

	class SelectDialog extends AlertDialog implements
			android.view.View.OnClickListener {
		TextView tvT,tv1, tv2, tv3, tv4, tv5, tv6;
		int Id;

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.menu_item);
			InitLayout();
		}

		@SuppressWarnings("deprecation")
		private void InitLayout() {
			tvT=(TextView) findViewById(R.id.menu_title);
			tv1 = (TextView) findViewById(R.id.menu_unsolved);
			tv2 = (TextView) findViewById(R.id.menu_implemented);
			tv3 = (TextView) findViewById(R.id.menu_reviewed);
			tv4 = (TextView) findViewById(R.id.menu_merged);
			tv5 = (TextView) findViewById(R.id.menu_verified);
			tv6 = (TextView) findViewById(R.id.menu_delete);
			tvT.setText(adapter.getItem(Id).toString()
					.split(Contants.COMMENT)[0]);
			tv1.setOnClickListener(this);
			tv2.setOnClickListener(this);
			tv3.setOnClickListener(this);
			tv4.setOnClickListener(this);
			tv5.setOnClickListener(this);
			tv6.setOnClickListener(this);
		}

		public SelectDialog(Context context, boolean cancelable,
				OnCancelListener cancelListener) {
			super(context, cancelable, cancelListener);
			// TODO Auto-generated constructor stub
		}

		public SelectDialog(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
		}

		protected SelectDialog(Context context, int theme) {

			super(context, theme);
			// TODO Auto-generated constructor stub
		}

		@SuppressWarnings("unchecked")
		@Override
		public void onClick(View v) {
			String jira_no;
			switch (v.getId()) {
			case R.id.menu_delete:
				jira_no = adapter.getItem(Id).toString()
						.split(Contants.COMMENT)[0];
				sqlHelper.deleteJira(jira_no);
				adapter.remove(adapter.getItem(Id));
				initTheJira();
				adapter.notifyDataSetChanged();
				SelectDialog.this.cancel();
				break;
			case R.id.menu_unsolved:
				jira_no = adapter.getItem(Id).toString()
				.split(Contants.COMMENT)[0];
				sqlHelper.updateStatus(jira_no,Contants.STATUS_OPEN);
				initTheJira();
				rg.check(rb1.getId());
				SelectDialog.this.cancel();
				break;
			case R.id.menu_implemented:
				jira_no = adapter.getItem(Id).toString()
				.split(Contants.COMMENT)[0];
				sqlHelper.updateStatus(jira_no,Contants.STATUS_IMPLEMENTED);
				initTheJira();
				rg.check(rb2.getId());
				SelectDialog.this.cancel();
				break;
			case R.id.menu_reviewed:
				jira_no = adapter.getItem(Id).toString()
				.split(Contants.COMMENT)[0];
				sqlHelper.updateStatus(jira_no,Contants.STATUS_REVIEWED);
				initTheJira();
				rg.check(rb3.getId());
				SelectDialog.this.cancel();
				break;
			case R.id.menu_merged:
				jira_no = adapter.getItem(Id).toString()
				.split(Contants.COMMENT)[0];
				sqlHelper.updateStatus(jira_no,Contants.STATUS_MERGERD);
				initTheJira();
				rg.check(rb4.getId());
				SelectDialog.this.cancel();
				break;
			case R.id.menu_verified:
				jira_no = adapter.getItem(Id).toString()
				.split(Contants.COMMENT)[0];
				sqlHelper.updateStatus(jira_no,Contants.STATUS_VERIFIED);
				initTheJira();
				rg.check(rb5.getId());
				SelectDialog.this.cancel();
				break;
			default:
				break;
			}
		}

	}

}
